﻿Ext.ns("Ext.ux.form");
Ext.ux.form.MyCombobox = Ext.extend(Ext.form.ComboBox, {
    initComponent: function () {
        Ext.ux.form.MyCombobox.superclass.initComponent.call(this);
    },
    collapse: function () {
        return;
    },
    collapseEx: function () {
        Ext.ux.form.MyCombobox.superclass.collapse.call(this);
    },
    collapseIf: function (e) {
        if (!this.isDestroyed && !e.within(this.wrap) && !e.within(this.list)) {
            this.collapseEx();
        }
    },
    onTriggerClick: function () {
        if (this.readOnly || this.disabled) {
            return;
        }
        if (this.isExpanded()) {
            this.collapseEx();
            this.el.focus();
        } else {
            this.onFocus({});
            if (this.triggerAction == 'all') {
                this.doQuery(this.allQuery, true);
            } else {
                this.doQuery(this.getRawValue());
            }
            this.el.focus();
        }
    },

    initEvents: function () {
        Ext.form.ComboBox.superclass.initEvents.call(this);


        this.keyNav = new Ext.KeyNav(this.el, {
            "up": function (e) {
                this.inKeyMode = true;
                this.selectPrev();
            },

            "down": function (e) {
                if (!this.isExpanded()) {
                    this.onTriggerClick();
                } else {
                    this.inKeyMode = true;
                    this.selectNext();
                }
            },

            "enter": function (e) {
                this.onViewClick();
            },

            "esc": function (e) {
                this.collapseEx();
            },

            "tab": function (e) {
                if (this.forceSelection === true) {
                    this.collapseEx();
                } else {
                    this.onViewClick(false);
                }
                return true;
            },

            scope: this,

            doRelay: function (e, h, hname) {
                if (hname == 'down' || this.scope.isExpanded()) {

                    var relay = Ext.KeyNav.prototype.doRelay.apply(this, arguments);
                    if (!Ext.isIE && Ext.EventManager.useKeydown) {

                        this.scope.fireKey(e);
                    }
                    return relay;
                }
                return true;
            },

            forceKeyDown: true,
            defaultEventAction: 'stopEvent'
        });
        this.queryDelay = Math.max(this.queryDelay || 10,
                this.mode == 'local' ? 10 : 250);
        this.dqTask = new Ext.util.DelayedTask(this.initQuery, this);
        if (this.typeAhead) {
            this.taTask = new Ext.util.DelayedTask(this.onTypeAhead, this);
        }
        if (!this.enableKeyEvents) {
            this.mon(this.el, 'keyup', this.onKeyUp, this);
        }
    }

});